package com.gengao.springbootcase.controller;

import com.gengao.springbootcase.pojo.Result;
import com.gengao.springbootcase.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.ResultMapResolver;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.tags.HtmlEscapeTag;

import javax.servlet.http.*;
import java.util.HashMap;

@RestController
@Slf4j
public class SessionController {
    @GetMapping("/c1")
    public Result cookie1(HttpServletResponse response){
        Cookie cookie=new Cookie("Login_Username","MikeG");
        response.addCookie(cookie);
        return Result.success(cookie);
    }

    @GetMapping("/c2")
    public Result cookie2(HttpServletRequest request){
        Cookie[] cookies=request.getCookies();
        for(Cookie cookie:cookies){
            if(cookie.getName().equals("Login_Username")){

                log.info("cookie:{}",cookie.getValue());
            }
        }
        return Result.success();
    }

    @GetMapping("/s1")
    public Result session1(HttpSession session){
        log.info("session,{}",session.hashCode());
        session.setAttribute("loginUser","Tom");
        return Result.success();
    }

    @GetMapping("/s2")
    public Result session2(HttpServletRequest request){
        HttpSession session=request.getSession();
        log.info("session,{}",session.hashCode());
        Object loginUser=session.getAttribute("loginUser");
        log.info("loginUser,{}",loginUser);
        return Result.success(loginUser);
    }

    @GetMapping("/jwt1")
    public Result jwt1(){
        HashMap<String,Object> claims=new HashMap<>();
        claims.put("LoginUserName","MikeG");
        claims.put("id","1");
        String jwt = JwtUtils.generateJwt(claims);
        return Result.success(jwt);
    }
}
